﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.CMS.SiteConverter.Parser.WebForm
{
    public class ListParser : ParserBase, IParser<IList>
    {
        #region IParser<IList> Members

        public void Parse(IList obj)
        {
            //declare
            var declare = "<%@ Control Language=\"C#\" Inherits=\"System.Web.Mvc.ViewUserControl<dynamic>\" %>" + Environment.NewLine;
            obj.StartNode.ParentNode.InsertAfter(NodeExtension.CreateNode(declare), obj.StartNode);

            var loopStartFormat = @"
<%foreach (var item in ViewBag.{0})
{{%>
";
            var blockFormat = @"<%=ViewHelper.Edit(item)%>";
            var fieldFormat = @"<%=ViewHelper.EditField(item, ""{0}"")%>"; //var fieldFormat = @"<%=item.{0}%>";
            var linkFormat = @"<%:Url.FrontUrl().PageUrl(""{0}"", new {{ UserKey = item.UserKey }})%>";
            var imageSrcFormat = @"<%:Url.Content(item.{0})%>";
            var loopEndFormat = @"
<%}%>";
            var pagerFormat = @"
<div class=""pager"">
    <%:Html.FrontHtml().Pager(ViewBag.{0})%>
</div>";

            Razor.ListParser.FillResult(this.Context, obj, this.EngineName, loopStartFormat, loopEndFormat, blockFormat, fieldFormat, linkFormat, imageSrcFormat, pagerFormat);
        }

        #endregion
    }
}
